<?php

include_once 'db.php';

$factory = new DbFactory();
$arr=array();

if(isset($_POST['azione']) && !empty($_POST['azione'])) {
	$action = $_POST['azione'];
	switch($action) {
		case 'base_mat' : 
			$arr = $factory->find("base_mat");
			break;
		case 'option1' :
			$base_mat=$_POST['base_mat'];
			$filter = "base_mat='".$base_mat."'";
			$arr = $factory->find("option1",$filter);
			break;
		case 'art' :
			$base_mat=$_POST['base_mat'];
			$option1=$_POST['option1'];
			$filter = "base_mat='".$base_mat."' AND option1='". $option1."'";
			$arr = $factory->find("art",$filter);		
			break;	
		case 'base_size' :
			$base_mat=$_POST['base_mat'];
			$option1=$_POST['option1'];
			$art=$_POST['art'];
			$filter = "base_mat='".$base_mat."' AND option1='". $option1."' AND art='" . $art."'";
			$arr = $factory->find("base_size",$filter);
			break;
		case 'stud_lenght' :
			$base_mat=$_POST['base_mat'];
			$option1=$_POST['option1'];
			$art=$_POST['art'];
			$base_size=$_POST['base_size'];
			$filter = "base_mat='".$base_mat."' AND option1='". $option1."' AND art='" . $art."' AND base_size=" . $base_size;
			$arr = $factory->find("stud_lenght",$filter);
			break;
		case 'stud_mat' :
			$base_mat=$_POST['base_mat'];
			$option1=$_POST['option1'];
			$art=$_POST['art'];
			$base_size=$_POST['base_size'];
			$stud_lenght=$_POST['stud_lenght'];
			$filter = "base_mat='".$base_mat."' AND option1='". $option1."' AND art='" . $art."' AND base_size=" . $base_size." AND stud_lenght=" . $stud_lenght;
			$arr = $factory->find("stud_mat",$filter);
			break;
		case 'stud_size' :
			$base_mat=$_POST['base_mat'];
			$option1=$_POST['option1'];
			$art=$_POST['art'];
			$base_size=$_POST['base_size'];
			$stud_lenght=$_POST['stud_lenght'];
			$stud_mat=$_POST['stud_mat'];
			$filter = "base_mat='".$base_mat."' AND option1='". $option1."' AND art='" . $art."' AND base_size=" . $base_size." AND stud_lenght=" 
					. $stud_lenght." AND stud_mat='" . $stud_mat."'";
			$arr = $factory->find("stud_size",$filter);
			break;
		case 'option2' :
			$base_mat=$_POST['base_mat'];
			$option1=$_POST['option1'];
			$art=$_POST['art'];
			$base_size=$_POST['base_size'];
			$stud_lenght=$_POST['stud_lenght'];
			$stud_mat=$_POST['stud_mat'];
			$stud_size=$_POST['stud_size'];
			$filter = "base_mat='".$base_mat."' AND option1='". $option1."' AND art='" . $art."' AND base_size=" 
					. $base_size." AND stud_lenght=" . $stud_lenght." AND stud_mat='" . $stud_mat."'"." AND stud_size='" . $stud_size."'";
			$arr = $factory->find("option2",$filter);
			break;
		case 'product' :
			$base_mat=$_POST['base_mat'];
			$option1=$_POST['option1'];
			$art=$_POST['art'];
			$base_size=$_POST['base_size'];
			$stud_lenght=$_POST['stud_lenght'];
			$stud_mat=$_POST['stud_mat'];
			$stud_size=$_POST['stud_size'];
			$option2=$_POST['option2'];
			$filter = "base_mat='".$base_mat."' AND option1='". $option1."' AND art='" . $art."' AND base_size=" 
					. $base_size." AND stud_lenght=" . $stud_lenght." AND stud_mat='" . $stud_mat."' AND stud_size='" . $stud_size."' AND option2='" . $option2."'";
			$arr = $factory->product("code, description",$filter);
			break;
		default:
			error_log("Azione non valida ".$action);
			$arr=Array();
	}
	echo json_encode($arr);
}

?>